package com.springboot.repository;

import com.springboot.domain.BtDemand;
import com.springboot.dto.TeamDemandDetailDTO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface DemandRepository extends JpaRepository<BtDemand,String> {

    @Query(value = "select bd from BtDemand  bd where bd.bankId= ?1 and bd.status = '未签约'",
            countQuery = "select count(bd.id) from BtDemand bd where bd.bankId= ?1 and bd.status = '未签约'" )
    Page<BtDemand> findDemandByBankId(String id, Pageable pageable);

    @Query("select bds from BtDemand bds where bds.platShown = ?1")
    Page<BtDemand> findDemands(Integer demandShown, Pageable pageable);

    @Query(value = "select  bd from BtDemand  bd left join bd.tasks bdt where bdt.receiver = :userId and bd.status = '已签约' and bdt.status=2 ",
             countQuery = "select  count(bd.id) from BtDemand bd left join bd.tasks  bdt where bdt.receiver = :userId and bd.status = '已签约' and bdt.status=2" )
    Page<BtDemand> findSignedListByUser(@Param("userId") String userId, Pageable pageable);

    @Query("select bd from BtDemand bd where bd.bankId = ?1 and bd.status = '已签约'")
    Page<BtDemand> findSignedListByBank(@Param("userId") String userId, Pageable pageable);

    @Query(value = "select bd from BtDemand bd left join bd.tasks bdt where bdt.receiver = ?1 and bdt.status = 2",
            countQuery = "select count (bd.id) from BtDemand bd left join bd.tasks bdt where bdt.receiver = ?1 and bdt.status = 2")
    Page<BtDemand> projectList(String userId, Pageable pageable);

    @Query(value = "select bd from BtDemand bd left join bd.authorities bda where bda.userId = ?1",
            countQuery = "select count (bd.id) from BtDemand bd left join bd.authorities bda  where bda.userId = ?1")
    List<BtDemand> findDemandByUser(String id);

    List<BtDemand> findByOutsourcerShown(Integer allCanSee);

    @Query(value = "select  bd from BtDemand  bd,BtTeam bt where bd.id = bt.demand and bd.status = '已签约'and bt.userId=:userId ",
            countQuery = "select count(bd.id) from BtDemand  bd,BtTeam bt where bd.id = bt.demand and bd.status = '已签约'and bt.userId=:userId" )
    Page<BtDemand> findDemandListByTeam(@Param("userId") String userId, Pageable pageable);

    @Query("select distinct new com.springboot.dto.TeamDemandDetailDTO(bt.id,bt.bankId,bt.type,bt.name,bt.projectSize,bt.description,bt.delivery,bt.money," +
            " bt.deadline,bt.language,bt.tecQualification,bt.busQualification,bt.specReq,bt.belongSystem,bt.belongProject,bt.paymentWay,bt.priority,bt.difficulty,"+
            " bt.status,bt.createTime,bt.modifyTime,bt.platShown,bt.outsourcerShown,bt.modifyTimes,bb.name)" +
            " from BtDemand bt,BtBank bb where bt.bankId=bb.id and bt.id = ?1")
    TeamDemandDetailDTO findDemandByTeamUserId (String id);

    @Query(value = "select  bd from BtDemand  bd left join bd.tasks bt where bt.receiver = :userId and bd.status = '已签约' and bt.status=2",
            countQuery = "select  count(bd.id) from BtDemand bd left join bd.tasks bt where bt.receiver = :userId and bd.status = '已签约' and bt.status=2" )
    Page<BtDemand> findSignedListByEnter(@Param("userId") String userId, Pageable pageable);

    Page<BtDemand> findByStatus(Pageable pageable, String unsigned);

    @Modifying
    @Query("update BtDemand bd set bd.views = ?1 where bd.id = ?2 ")
    void addViews(int total, String id);

    @Query("select bd from BtDemand bd where bd.name like concat('%',?1,'%')")
    List<BtDemand> findDemand(String name);
}
